/*
 * @Author: your name
 * @Date: 2022-01-06 17:59:16
 * @LastEditTime: 2022-04-12 18:20:48
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \web\src\network\index.js
 */
import axios from 'axios';
import Cookie from '../assets/common/cookie';
import { message } from 'antd';
const http = axios.create({
  // baseURL: 'http://127.0.0.1:3000',
  baseURL: 'https://qc8dt2.app.cloudendpoint.cn',
})

// 添加请求拦截器
http.interceptors.request.use(config => {
  // 在发送请求之前做些什么
  const token = Cookie.get("token");
  if (token) {
    config.headers.Authorization = 'Bearer ' + token;
  }
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

http.interceptors.response.use( res => {
  return res.data;
}, err =>{
  if (err.response.data.error) {
    message.error(err.response.data.error);
  }
  if(err.response.status === 401) {
    Cookie.remove('userInfo');
    window.location = "http://localhost:3000/login";
  }
  return null;
})

export default http